CSS for scrollable output & Header colors
Turning scientific / Exponential numbers off
options(scipen = 999)
library(tidyverse)
library(ggthemes)
library(lubridate)
library(covid19.analytics)
library(data.table)
library(plotly)
library(gghighlight)
library(zoo) # this is for moving averages
# library(ggThemeAssist)
library(glue)
library(scales)
theme_viny_bright <- function(){
library(ggthemes)
ggthemes::theme_fivethirtyeight() %+replace%
theme(
axis.title = element_text(),
axis.text = element_text(size = 13),
legend.text = element_text(size = 10),
panel.background = element_rect(fill = "white"),
plot.background = element_rect(fill = "white"),
strip.background = element_blank(),
legend.background = element_rect(fill = NA),
legend.key = element_rect(fill = NA),
plot.title = element_text(hjust = 0.5,
size = 19,
face = "bold"),
plot.subtitle = element_text(hjust = 0.5, colour = "maroon")
)
}
theme_set(theme_viny_bright())
df_all_cases <- covid19.analytics::covid19.data()
df_all_cases %>% head()
df_all_cases <- df_all_cases %>%
rename(Incident_Rate = Incidence_Rate,
Case_Fatality_Ratio = Case.Fatality_Ratio
)
Case_Fatality_Ratio
Deaths * 100 / Confirmed
2067 * 100 / 49817
df_all_cases %>%
arrange(desc(Incident_Rate))
dim(df_all_cases)
df_all_cases %>%
slice_max(order_by = Case_Fatality_Ratio, n = 15)
library(gapminder)
gapminder %>% head()
continent_lookup <- gapminder %>%
filter(year == max(year)) %>%
select(-year)
continent_lookup
df_all_cases <- df_all_cases %>%
left_join(continent_lookup, by = c("Country_Region" = "country"))
df_all_cases
df_all_cases <- df_all_cases %>%
select(Province_State, Country_Region, continent, pop, gdpPercap, lifeExp,
Confirmed, Deaths, Recovered, Active, Incident_Rate, Case_Fatality_Ratio, everything())
df_all_cases
df_all_cases <- df_all_cases %>%
mutate(continent = as.character(continent))
df_all_cases
df_all_cases[which(is.na(df_all_cases$continent)), "continent"] <- "Unknown"
df_all_cases
df_all_cases <- df_all_cases %>%
mutate(continent = as.factor(continent))
table(df_all_cases$continent)
df_all_cases %>%
filter(continent == "Unknown") %>%
select(Country_Region) %>%
unique()
df_all_cases[df_all_cases$Country_Region == "US", "continent"] <- "Americas"
df_all_cases[df_all_cases$Country_Region == "Bhutan", "continent"] <- "Asia"
df_all_cases[df_all_cases$Country_Region == "Brunei", "continent"] <- "Asia"
df_all_cases[df_all_cases$Country_Region == "Burma", "continent"] <- "Asia"
df_all_cases[df_all_cases$Country_Region == "Maldives", "continent"] <- "Asia"
df_all_cases[df_all_cases$Country_Region == "Qatar", "continent"] <- "Asia"
df_all_cases[df_all_cases$Country_Region == "United Arab Emirates", "continent"] <- "Asia"
df_all_cases[df_all_cases$Country_Region == "Taiwan*", "continent"] <- "Asia"
df_all_cases[df_all_cases$Country_Region == "Monaco", "continent"] <- "Europe"
df_all_cases[df_all_cases$Country_Region == "Luxembourg", "continent"] <- "Europe"
df_all_cases[df_all_cases$Country_Region == "Lithuania", "continent"] <- "Europe"
df_all_cases[df_all_cases$Country_Region == "Russia", "continent"] <- "Europe"
df_all_cases[df_all_cases$Country_Region == "Ukraine", "continent"] <- "Europe"
df_all_cases[df_all_cases$Country_Region == "Tajikistan", "continent"] <- "Europe"
df_all_cases[df_all_cases$Country_Region == "Uzbekistan", "continent"] <- "Europe"
df_all_cases[df_all_cases$Country_Region == "Korea, South", "continent"] <- "Asia"
df_all_cases[df_all_cases$Country_Region == "Czechia", "continent"] <- "Europe"
df_all_cases[df_all_cases$Country_Region == "Andorra", "continent"] <- "Europe"
df_all_cases[df_all_cases$Country_Region == "Antigua and Barbuda", "continent"] <- "Americas"
df_all_cases[df_all_cases$Country_Region == "Armenia", "continent"] <- "Asia"
df_all_cases[df_all_cases$Country_Region == "Yemen", "continent"] <- "Asia"
df_all_cases[df_all_cases$Country_Region == "Cyprus", "continent"] <- "Europe"
df_all_cases[df_all_cases$Country_Region == "US", "pop"] <- 330052960
df_all_cases[df_all_cases$Country_Region == "Bhutan", "pop"] <- 771608
df_all_cases[df_all_cases$Country_Region == "Brunei", "pop"] <- 437479
df_all_cases[df_all_cases$Country_Region == "Burma", "pop"] <- 54409800
df_all_cases[df_all_cases$Country_Region == "Maldives", "pop"] <- 540544
df_all_cases[df_all_cases$Country_Region == "Qatar", "pop"] <- 2881053
df_all_cases[df_all_cases$Country_Region == "United Arab Emirates", "pop"] <- 9890402
df_all_cases[df_all_cases$Country_Region == "Taiwan*", "pop"] <- 23816775
df_all_cases[df_all_cases$Country_Region == "Monaco", "pop"] <- 39242
df_all_cases[df_all_cases$Country_Region == "Luxembourg", "pop"] <- 625978
df_all_cases[df_all_cases$Country_Region == "Lithuania", "pop"] <- 2722289
df_all_cases[df_all_cases$Country_Region == "Russia", "pop"] <- 145934462
df_all_cases[df_all_cases$Country_Region == "Ukraine", "pop"] <- 43733762
df_all_cases[df_all_cases$Country_Region == "Tajikistan", "pop"] <- 9537645
df_all_cases[df_all_cases$Country_Region == "Uzbekistan", "pop"] <- 33469203
df_all_cases[df_all_cases$Country_Region == "Korea, South", "pop"] <- 51290514
df_all_cases[df_all_cases$Country_Region == "Czechia", "pop"] <- 10718488
df_all_cases[df_all_cases$Country_Region == "Andorra", "pop"] <- 76177
df_all_cases[df_all_cases$Country_Region == "Antigua and Barbuda", "pop"] <- 96286
df_all_cases[df_all_cases$Country_Region == "Armenia", "pop"] <- 2970000
df_all_cases[df_all_cases$Country_Region == "Yemen", "pop"] <- 28500000
df_all_cases[df_all_cases$Country_Region == "Cyprus", "pop"] <- 876000
df_all_cases[df_all_cases$Country_Region == "US", "gdpPercap"] <- 65297.5
df_all_cases[df_all_cases$Country_Region == "Bhutan", "gdpPercap"] <- 3316.2
df_all_cases[df_all_cases$Country_Region == "Brunei", "gdpPercap"] <- 31086.8
df_all_cases[df_all_cases$Country_Region == "Burma", "gdpPercap"] <- 1407.8
df_all_cases[df_all_cases$Country_Region == "Maldives", "gdpPercap"] <- 10626.5
df_all_cases[df_all_cases$Country_Region == "Qatar", "gdpPercap"] <- 62088.1
df_all_cases[df_all_cases$Country_Region == "United Arab Emirates", "gdpPercap"] <- 43103.3
df_all_cases[df_all_cases$Country_Region == "Taiwan*", "gdpPercap"] <- 26910.229
df_all_cases[df_all_cases$Country_Region == "Monaco", "gdpPercap"] <- 185829.0
df_all_cases[df_all_cases$Country_Region == "Luxembourg", "gdpPercap"] <- 114704.6
df_all_cases[df_all_cases$Country_Region == "Lithuania", "gdpPercap"] <- 19601.9
df_all_cases[df_all_cases$Country_Region == "Russia", "gdpPercap"] <- 11585.0
df_all_cases[df_all_cases$Country_Region == "Ukraine", "gdpPercap"] <- 3659.0
df_all_cases[df_all_cases$Country_Region == "Tajikistan", "gdpPercap"] <- 870.8
df_all_cases[df_all_cases$Country_Region == "Uzbekistan", "gdpPercap"] <- 1724.8
df_all_cases[df_all_cases$Country_Region == "Korea, South", "gdpPercap"] <- 31362.75
df_all_cases[df_all_cases$Country_Region == "Czechia", "gdpPercap"] <- 23078.57
df_all_cases[df_all_cases$Country_Region == "Andorra", "gdpPercap"] <- 42029.76
df_all_cases[df_all_cases$Country_Region == "Antigua and Barbuda", "gdpPercap"] <- 16726.98
df_all_cases[df_all_cases$Country_Region == "Armenia", "gdpPercap"] <- 4212.07
df_all_cases[df_all_cases$Country_Region == "Yemen", "gdpPercap"] <- 944.41
df_all_cases[df_all_cases$Country_Region == "Cyprus", "gdpPercap"] <- 28159.30
df_all_cases[df_all_cases$Country_Region == "US", "lifeExp"] <- 79.11
df_all_cases[df_all_cases$Country_Region == "Bhutan", "lifeExp"] <- 72.77
df_all_cases[df_all_cases$Country_Region == "Brunei", "lifeExp"] <- 76.35
df_all_cases[df_all_cases$Country_Region == "Burma", "lifeExp"] <- 67.78
df_all_cases[df_all_cases$Country_Region == "Maldives", "lifeExp"] <- 79.89
df_all_cases[df_all_cases$Country_Region == "Qatar", "lifeExp"] <- 80.73
df_all_cases[df_all_cases$Country_Region == "United Arab Emirates", "lifeExp"] <- 78.46
df_all_cases[df_all_cases$Country_Region == "Taiwan*", "lifeExp"] <- 81.04
df_all_cases[df_all_cases$Country_Region == "Monaco", "lifeExp"] <- 85.8
df_all_cases[df_all_cases$Country_Region == "Luxembourg", "lifeExp"] <- 82.79
df_all_cases[df_all_cases$Country_Region == "Lithuania", "lifeExp"] <- 76.41
df_all_cases[df_all_cases$Country_Region == "Russia", "lifeExp"] <- 72.99
df_all_cases[df_all_cases$Country_Region == "Ukraine", "lifeExp"] <- 72.50
df_all_cases[df_all_cases$Country_Region == "Tajikistan", "lifeExp"] <- 71.76
df_all_cases[df_all_cases$Country_Region == "Uzbekistan", "lifeExp"] <- 72.04
df_all_cases[df_all_cases$Country_Region == "Korea, South", "lifeExp"] <- 82.63
df_all_cases[df_all_cases$Country_Region == "Czechia", "lifeExp"] <- 79.48
df_all_cases[df_all_cases$Country_Region == "Andorra", "lifeExp"] <- 83
df_all_cases[df_all_cases$Country_Region == "Antigua and Barbuda", "lifeExp"] <- 76.75
df_all_cases[df_all_cases$Country_Region == "Armenia", "lifeExp"] <- 74.80
df_all_cases[df_all_cases$Country_Region == "Yemen", "lifeExp"] <- 66.09
df_all_cases[df_all_cases$Country_Region == "Cyprus", "lifeExp"] <- 80.67
df_all_cases %>%
group_by(Country_Region, continent) %>%
summarise(Confirmed = sum(Confirmed),
Active = sum(Active),
Deaths = sum(Deaths),
Recovered = sum(Recovered)) %>%
mutate(Case_Fatality_Ratio = Deaths * 100 / Confirmed)
continent_lookup <- df_all_cases %>%
select(Country_Region, continent)
df_all_cases %>%
group_by(Country_Region, continent) %>%
summarise(Confirmed = sum(Confirmed),
Active = sum(Active),
Deaths = sum(Deaths),
Recovered = sum(Recovered)) %>%
ungroup() %>%
mutate(Case_Fatality_Ratio = Deaths * 100 / Confirmed) %>%
slice_max(order_by = Case_Fatality_Ratio, n = 15)
# top_n( n = 15, wt = Case_Fatality_Ratio) %>%
# arrange(desc(Case_Fatality_Ratio))
df_all_cases_sum <- df_all_cases %>%
group_by(Country_Region) %>%
summarise(continent = first(continent),
pop = last(pop),
gdpPercap = last(gdpPercap),
Confirmed = sum(Confirmed),
Active = sum(Active),
Deaths = sum(Deaths),
Recovered = sum(Recovered)) %>%
ungroup() %>%
mutate(Case_Fatality_Ratio = Deaths * 100 / Confirmed) %>%
arrange(desc(Confirmed))
df_all_cases_sum
df_all_cases %>%
select(Country_Region, pop, gdpPercap, lifeExp) %>%
distinct()
df_all_cases_sum %>%
ggplot(aes( x = Case_Fatality_Ratio, y = Confirmed)) +
geom_point(aes(size = Confirmed, col = continent))
df_all_cases_sum %>%
ggplot(aes( x = Case_Fatality_Ratio, y = Confirmed)) +
geom_point(aes(size = Confirmed, col = continent)) +
facet_wrap(~continent) +
theme_bw()
df_all_cases_sum %>%
filter(Case_Fatality_Ratio < 10) %>%
ggplot(aes( x = Case_Fatality_Ratio, y = Confirmed)) +
geom_point(aes(size = Confirmed, col = continent))
df_all_cases_sum %>%
filter(Case_Fatality_Ratio < 10) %>%
ggplot(aes( x = Case_Fatality_Ratio, y = Confirmed)) +
geom_point(aes(size = Confirmed, col = continent)) +
theme_bw() +
facet_wrap(~continent)
top_10 <- df_all_cases_sum %>%
top_n(n = 10, wt = Confirmed) %>%
pull(Country_Region)
top_10
top_20 <- df_all_cases_sum %>%
top_n(n = 20, wt = Confirmed) %>%
pull(Country_Region)
top_20
df_ts_recovered <- covid19.data("ts-recovered")
df_ts_recovered %>% head()
df_ts_confirmed <- covid19.data("ts-confirmed")
df_ts_confirmed %>% head()
df_ts_deaths <- covid19.data("ts-deaths")
df_ts_deaths %>% head()
df_ts_confirmed %>% select(last_col())
df_gather_confirmed <- gather(data = df_ts_confirmed, key = "Date", value = "Confirmed", -c("Province.State","Lat","Long", "Country.Region"))
df_gather_confirmed %>% head()
df_gather_recovered <- gather(data = df_ts_recovered, key = "Date", value = "Recovered", -c("Province.State","Lat","Long", "Country.Region"))
df_gather_recovered %>% head()
df_gather_deaths <- gather(data = df_ts_deaths, key = "Date", value = "Deaths", -c("Province.State","Lat","Long", "Country.Region"))
df_gather_deaths %>% head()
grouped_cases_df <- df_gather_confirmed %>%
full_join(df_gather_recovered, by = c("Province.State", "Country.Region",
"Lat", "Long", "Date"))
grouped_cases_df
grouped_cases_df %>%
arrange(desc(Confirmed))
grouped_cases_df <- grouped_cases_df %>%
full_join(df_gather_deaths,by = c("Province.State", "Country.Region", "Lat", "Long", "Date"))
grouped_cases_df
grouped_cases_df <- grouped_cases_df %>%
mutate(Active = Confirmed - (Recovered + Deaths),
Date = as.Date(Date))
grouped_cases_df
grouped_cases_df <- grouped_cases_df %>%
mutate(Case_Fatality_Ratio = Deaths * 100 / Confirmed)
grouped_cases_df
is.na(grouped_cases_df$Case_Fatality_Ratio) %>% head()
Replacing missing values with 0
grouped_cases_df[which(is.na(grouped_cases_df$Case_Fatality_Ratio)), "Case_Fatality_Ratio"] <- 0
grouped_cases_df <- grouped_cases_df %>%
mutate(Province.State = as.factor(Province.State),
Country.Region = as.factor(Country.Region))
df_all_cases_sum %>% head()
grouped_cases_df <- left_join(x = grouped_cases_df,
y = df_all_cases_sum %>% select(Country_Region, continent),
by = c("Country.Region" = "Country_Region")
)
grouped_cases_df
grouped_cases_df %>% head()
grouped_province_cases_df <- grouped_cases_df
grouped_province_cases_df
grouped_cases_df %>%
filter(Date == max(Date, na.rm = TRUE)) %>%
count(n = Country.Region)
grouped_cases_df <- grouped_cases_df %>%
select(Country.Region, continent, Date, Confirmed, Deaths, Active, Recovered) %>%
group_by(Country.Region, continent, Date) %>%
summarise(Confirmed = sum(Confirmed),
Deaths = sum(Deaths),
Active = sum(Active),
Recovered = sum(Recovered)
) %>%
mutate(Case_Fatality_Ratio = Deaths / Confirmed * 100) %>%
ungroup()
grouped_cases_df
grouped_cases_df[which(is.na(grouped_cases_df$Case_Fatality_Ratio)), "Case_Fatality_Ratio"] <- 0
grouped_cases_df %>% head()
grouped_cases_df <- grouped_cases_df %>%
mutate(Case_Fatality_Ratio = round(Case_Fatality_Ratio, digits = 2),
Recovery_Rate = Recovered / Confirmed * 100) %>%
# Replacing NaN with 100 as when the cases are 0, recovery remains 0
mutate(Recovery_Rate = replace(Recovery_Rate, is.na(Recovery_Rate), 0) %>% round(digits = 2))
grouped_cases_df
grouped_cases_df <- grouped_cases_df %>%
group_by(Country.Region, Date) %>%
summarise(continent = first(continent),
Confirmed = sum(Confirmed),
Deaths = sum(Deaths),
Active = sum(Active),
Recovered = sum(Recovered),
Case_Fatality_Ratio = sum(Case_Fatality_Ratio),
Recovery_Rate = sum(Recovery_Rate)) %>%
mutate(Daily_cases = (Confirmed - lag(Confirmed, default = 0)),
Daily_deaths = Deaths - lag(Deaths, default = 0),
Daily_recovered = Recovered - lag(Recovered, default = 0)
) %>%
ungroup()
grouped_cases_df
grouped_cases_df %>%
filter(Date == max(Date, na.rm = TRUE)) %>%
arrange(desc(Case_Fatality_Ratio)) %>%
head(n = 40) %>%
ggplot(aes(x = reorder(Country.Region, Case_Fatality_Ratio),
y = Case_Fatality_Ratio,
fill = continent)) +
geom_col() +
theme_bw() +
theme(axis.text.x = element_text(angle = 90) ) +
coord_flip() +
labs(title = "Top 40 CFR Countries",
caption = "created by ViSa!!")
grouped_cases_df %>%
filter(Date == max(Date, na.rm = TRUE)) %>%
arrange(desc(Case_Fatality_Ratio)) %>%
head(n = 40) %>%
ggplot(aes(x = reorder(Country.Region, Case_Fatality_Ratio),
y = Case_Fatality_Ratio,
fill = continent)) +
geom_col() +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90) ) +
coord_flip() +
facet_wrap(~continent) +
labs(title = "Top 40 CFR Countries",
caption = "created by ViSa!!")
grouped_cases_df %>%
ggplot(aes(x = Date,
y = Case_Fatality_Ratio,
col = continent)) +
geom_path() +
theme_bw() +
theme(axis.text.x = element_text(angle = 90) ) +
facet_wrap(~continent) +
labs(title = "CFR of all Countries for continent comparison",
caption = "created by Visa!!")
grouped_cases_df %>%
ggplot(aes(x = Date,
y = Case_Fatality_Ratio,
col = continent)) +
geom_path() +
theme_bw() +
theme(axis.text.x = element_text(angle = 90) ) +
labs(title = "CFR of all Countries based on continent",
caption = "created by Visa!!")
grouped_cases_df %>% head()
grouped_cases_df %>%
filter(Date == max(Date, na.rm = TRUE)) %>%
group_by(continent) %>%
top_n(n =10, wt = Case_Fatality_Ratio) %>%
ungroup() %>%
select(Country.Region, continent) %>%
unique() %>%
pull(continent) %>%
table()
top10_CFR <- grouped_cases_df %>%
filter(Date == max(Date, na.rm = TRUE)) %>%
group_by(continent) %>%
top_n(n =10, wt = Case_Fatality_Ratio) %>%
ungroup() %>%
pull(Country.Region)
top10_CFR
grouped_cases_df %>%
filter(Country.Region %in% top10_CFR) %>%
ggplot(aes(x = Date, y = Case_Fatality_Ratio, col = continent)) +
geom_path() +
theme_bw() +
facet_wrap(~continent)
range(grouped_cases_df$Case_Fatality_Ratio)
quantiles use from: https://www.dummies.com/programming/r/how-to-check-quantiles-in-r/
quantile(grouped_cases_df$Case_Fatality_Ratio, probs = c(.33,.66,1), na.rm = TRUE )
quantile(grouped_cases_df$Case_Fatality_Ratio, probs = .33)
grouped_cases_df$CFR_level <- cut(grouped_cases_df$Case_Fatality_Ratio,
breaks = c(-1,
quantile(grouped_cases_df$Case_Fatality_Ratio, probs = .33),
quantile(grouped_cases_df$Case_Fatality_Ratio, probs = .66),
quantile(grouped_cases_df$Case_Fatality_Ratio, probs = 1)
),
labels = c("Low","Med","High"))
grouped_cases_df$CFR_level <- cut(grouped_cases_df$Case_Fatality_Ratio,
breaks = c(-1,0.5004858,2.3189270,100),
labels = c("Low","Med","High"))
grouped_cases_df %>%
filter(Country.Region == "India")
grouped_cases_df %>%
filter(Country.Region == "India") %>%
select(CFR_level) %>%
table()
grouped_cases_df %>%
filter(Country.Region == "India") %>%
ggplot(aes(x = Date, y = Confirmed, col = CFR_level)) +
geom_path() +
labs(title = "CFR level(Low,Med,High) of India wrt Time",
caption = "created by ViSa!!")
grouped_cases_df %>%
filter(Country.Region == "India") %>%
count(Date) %>%
filter(n > 1)
grouped_cases_df %>%
filter(Country.Region == "India") %>%
ggplot(aes(x = Date, y = Confirmed, col = CFR_level)) +
geom_line() +
labs(title = "CFR level(Low,Med,High) of India wrt Time",
caption = "created by ViSa!!")
solution is to use group = 1 from: https://stackoverflow.com/questions/65422773/getting-multiple-lines-in-geom-line-time-graph-when-used-with-another-factor-c/65422835#65422835
grouped_cases_df %>%
filter(Country.Region == "India") %>%
ggplot(aes(x = Date, y = Confirmed, col = CFR_level, group = 1)) +
geom_line() +
labs(title = "CFR level(Low,Med,High) of India wrt Time",
caption = "created by ViSa!!")
grouped_cases_df %>%
filter(Country.Region %in% top_10) %>%
ggplot(aes(x = Date, y = Confirmed, col = CFR_level, group = 1)) +
geom_line() +
facet_wrap(~continent)
grouped_cases_df %>%
filter(Country.Region %in% top_10) %>%
ggplot(aes(x = Date, y = Confirmed, col = CFR_level, group = 1)) +
geom_path() +
facet_wrap(~continent) +
labs(title = "CFR level of top10 countries wrt Time split by continent",
caption = "created by ViSa!!")
grouped_cases_df %>%
filter(Country.Region %in% top_10) %>%
ggplot(aes(x = Date, y = Confirmed, col = CFR_level, group = 1)) +
geom_line() #+
# facet_wrap(~continent)
p1_cfr <- grouped_cases_df %>%
filter(Country.Region %in% top_10) %>%
ggplot(aes(x = Date, y = Confirmed, col = CFR_level, group = Country.Region, label = continent)) +
geom_path(size = 1.2) +
labs(title = "CFR level of top10 countries wrt Time",
caption = "created by ViSa!!")
p1_cfr
library(plotly)
library(streamgraph)
ggplotly(p1_cfr)
grouped_cases_df %>%
filter(Country.Region %in% top_10) %>%
streamgraph(date = Date, key = "Country.Region", value = CFR_level) %>%
sg_fill_brewer("PuOr") %>%
sg_title(title = "CFR Level of top 10 countries wrt time")
library(highcharter)
grouped_cases_df %>%
filter(Date == max(Date, na.rm = TRUE)) %>%
group_by(Country.Region, continent) %>%
summarise(Confirmed = sum(Confirmed)) %>%
arrange(desc(Confirmed)) %>%
hchart("heatmap", hcaes(x = Country.Region, y = continent, value = Confirmed, color = Confirmed)) %>%
hc_title(text = "Confirmed cases across globe split by continent")
grouped_cases_df %>%
filter(Date == max(Date, na.rm = TRUE)) %>%
group_by(Country.Region, continent) %>%
summarise(Confirmed = sum(Confirmed)) %>%
arrange(desc(Confirmed)) %>%
hchart("treemap", hcaes(x = Country.Region, value = Confirmed, color = Confirmed)) %>%
hc_title(text = "Confirmed cases across globe")
from: https://www.kaggle.com/nulldata/beginners-guide-to-highchart-visual-in-r
grouped_cases_df %>%
filter(Date == max(Date, na.rm = TRUE)) %>%
group_by(Country.Region, continent) %>%
summarise(Confirmed = sum(Confirmed)) %>%
arrange(desc(Confirmed)) %>%
hchart("treemap", hcaes(x = Country.Region, value = Confirmed, color = Confirmed)) %>%
hc_colorAxis(stops = color_stops(colors = viridis::inferno(10))) %>%
hc_title(text = "Confirmed cases across globe")
grouped_cases_df %>%
filter(Country.Region %in% top_10) %>%
head(n = 30)
grouped_cases_df %>%
filter(Country.Region %in% top_10) %>%
summary()
grouped_cases_df %>% str()
grouped_cases_df %>%
filter(Country.Region %in% top_10) %>%
str()
grouped_cases_df %>%
filter(Country.Region %in% top_10) %>%
hchart(type = "line", hcaes(x = Date, y = Confirmed, group = Country.Region, color = CFR_level)) %>%
hc_title(text = "Confirmed cases of top 10 countries across globe")
grouped_cases_df %>%
filter(Country.Region %in% top_10) %>%
hchart(type = "line", hcaes(x = Date, y = Confirmed, group = Country.Region, color = CFR_level)) %>%
hc_add_theme(hc_theme_handdrawn()) %>%
hc_caption(text = "chart created by ViSa :)")
grouped_cases_df %>%
filter(Country.Region %in% top_10) %>%
hchart(type = "line", hcaes(x = Date, y = Confirmed, group = Country.Region, color = CFR_level)) %>%
hc_add_theme(hc_theme_db()) %>%
hc_title(text = "Confirmed cases of top 10 countries across globe") %>%
hc_caption(text = "chart created by ViSa :)")
grouped_cases_df %>%
filter(Country.Region %in% top_10) %>%
hchart(type = "line", hcaes(x = Date, y = log(Confirmed),
group = Country.Region, color = CFR_level)) %>%
hc_add_theme(hc_theme_smpl()) %>%
hc_title(text = "Confirmed cases of top 10 countries across globe") %>%
hc_caption(text = "chart created by ViSa :)")
grouped_cases_df %>%
filter(Country.Region %in% top_10) %>%
hchart(type = "line", hcaes(x = Date, y = Confirmed, color = CFR_level)) %>%
hc_title(text = "Confirmed cases of top 10 countries across globe")
colored line in reply from
grouped_cases_df %>%
filter(Country.Region %in% c("France","India")) %>%
hchart(type = "coloredline",
hcaes(x = Date, y = Confirmed, group = Country.Region, segmentColor = CFR_level)) %>%
hc_add_series(name = "Low",
color = "green", marker = list(symbol = "line")) %>%
hc_add_series(name = "Med",
color = "yellow", marker = list(symbol = "line")) %>%
hc_add_series(name = "High",
color = "red", marker = list(symbol = "line")) %>%
hc_add_dependency("plugins/multicolor_series.js")
from: https://stackoverflow.com/questions/65427527/how-to-change-color-of-line-chart-in-highchart-based-on-a-categorical-column-in https://jkunst.com/highcharter/articles/modules.html
grouped_cases_df %>%
rename(Country_Region = Country.Region) %>%
filter(Country_Region %in% top_10) %>%
mutate(CFR_col = case_when(CFR_level == "Low" ~ "#DAF7A6",
CFR_level == "Med" ~ "#4f86b1",
TRUE ~ "#FFC300"),
Case_Fatality_Ratio = round(Case_Fatality_Ratio, digits = 3)
) %>%
hchart(type = "coloredline", showInLegend = F,
hcaes(x = Date, y = Confirmed, group = Country_Region, segmentColor = CFR_col)) %>%
hc_add_dependency("plugins/multicolor_series.js") %>%
hc_tooltip(#shared = TRUE,
# borderColor = "black",
pointFormat = "{point.Country_Region}, {point.continent} <br>
CFR Level: {point.CFR_level}, {point.Case_Fatality_Ratio} <br>"
) %>%
hc_title(text = "Top 10 Covid confirmed cases countries & colored with CFR Levels") %>%
hc_caption(text = "chart created by ViSa :)")
grouped_cases_df %>%
rename(Country_Region = Country.Region) %>%
filter(Country_Region %in% top_10) %>%
mutate(CFR_col = case_when(CFR_level == "Low" ~ "#DAF7A6",
CFR_level == "Med" ~ "#4f86b1",
TRUE ~ "#FFC300"),
Case_Fatality_Ratio = round(Case_Fatality_Ratio, digits = 3)
) %>%
hchart(type = "coloredline", showInLegend = F,
hcaes(x = Date, y = Confirmed, group = Country_Region, segmentColor = CFR_col)) %>%
hc_add_dependency("plugins/multicolor_series.js") %>%
hc_tooltip(#shared = TRUE,
# borderColor = "black",
pointFormat = "{point.Country_Region}, {point.continent} <br>
CFR Level: {point.CFR_level}, {point.Case_Fatality_Ratio} <br>"
) %>%
hc_title(text = "Top 10 Covid confirmed cases countries & colored with CFR Levels") %>%
hc_add_theme(hc_theme_chalk()) %>%
hc_caption(text = "chart created by ViSa :)")
grouped_cases_df %>%
rename(Country_Region = Country.Region) %>%
filter(Country_Region %in% top_10) %>%
mutate(CFR_col = case_when(CFR_level == "Low" ~ "#DAF7A6",
CFR_level == "Med" ~ "#4f86b1",
TRUE ~ "#FFC300"),
Case_Fatality_Ratio = round(Case_Fatality_Ratio, digits = 3)
) %>%
hchart(type = "coloredline",
hcaes(x = Date, y = Confirmed, group = Country_Region, segmentColor = CFR_col)) %>%
hc_add_dependency("plugins/multicolor_series.js") %>%
hc_tooltip(shared = TRUE,
# borderColor = "black",
pointFormat = "{point.Country_Region}, {point.continent},
CFR Level: {point.CFR_level}, {point.Case_Fatality_Ratio} <br>"
) %>%
hc_title(text = "Top 10 Covid confirmed cases countries & colored with CFR Levels") %>%
hc_add_theme(hc_theme_monokai()) %>%
hc_yAxis(type = "logarithmic") %>%
hc_caption(text = "chart created by ViSa :)")
df_stack <- gather(data = grouped_cases_df, key = "Cases_type", value = Cases_count,
-c("Country.Region", "continent", Date,
Case_Fatality_Ratio, Recovery_Rate, CFR_level))
df_stack
table(df_stack$Cases_type)
df_stack <- df_stack %>%
mutate(Cases_type = as.factor(Cases_type),
Cases_count = as.numeric(Cases_count),
Date = as.Date(Date))
df_stack
df_stack %>%
filter(Date == max(Date, na.rm = TRUE))
df_stack <- df_stack %>%
mutate(Case_Fatality_Ratio = round(Case_Fatality_Ratio, digits = 2))
df_stack
top15_confirmed <- df_stack %>%
filter(Cases_type == "Confirmed",
Date == max(Date, na.rm = TRUE)) %>%
arrange(desc(Cases_count)) %>%
head(n = 15) %>%
pull(Country.Region)
top15_confirmed
top15_active <- df_stack %>%
filter(Cases_type == "Active",
Date == max(Date, na.rm = TRUE)) %>%
arrange(desc(Cases_count)) %>%
head(n = 15) %>%
pull(Country.Region)
top15_active
Based on Top 15 confirmed
df_stack %>%
group_by(Country.Region, continent, Date, Cases_type) %>%
summarise(Cases_count = sum(Cases_count, na.rm = TRUE)) %>%
ungroup() %>%
filter(Country.Region %in% top15_confirmed,
!Cases_type %in% c("Confirmed","Daily_cases","Daily_deaths","Daily_recovered"))
df_stack %>%
group_by(Country.Region, continent, Date, Cases_type) %>%
summarise(Cases_count = sum(Cases_count, na.rm = TRUE)) %>%
ungroup() %>%
filter(Country.Region %in% top15_confirmed,
!Cases_type %in% c("Confirmed","Daily_cases","Daily_deaths","Daily_recovered")) %>%
ggplot(aes(x = reorder(Country.Region,Cases_count),
y = Cases_count,
fill = as.factor(Cases_type)
)) +
geom_col() +
scale_y_continuous(labels = scales::comma_format()) +
coord_flip() +
labs(title = "All type cases of top countries",
subtitle = "Top countries is based on Highest Confirmed cases till date",
caption = "created by ViSa!!")
df_stack %>%
group_by(Country.Region, continent, Date, Cases_type) %>%
summarise(Cases_count = sum(Cases_count, na.rm = TRUE)) %>%
ungroup() %>%
filter(Country.Region %in% top15_confirmed,
!Cases_type %in% c("Confirmed","Daily_cases","Daily_deaths","Daily_recovered")) %>%
ggplot(aes(x = reorder(Country.Region,Cases_count),
y = Cases_count,
group = Country.Region,
fill = as.factor(Cases_type)
)) +
geom_col() +
scale_y_continuous(labels = scales::comma_format()) +
coord_flip() +
labs(title = "All type cases of top countries",
subtitle = "Top countries is based on Highest Confirmed cases till date",
caption = "created by ViSa!!")
df_stack %>%
group_by(Country.Region, continent, Date, Cases_type) %>%
summarise(Cases_count = sum(Cases_count, na.rm = TRUE)) %>%
ungroup() %>%
filter(Country.Region %in% top15_confirmed,
!Cases_type %in% c("Confirmed","Daily_cases","Daily_deaths","Daily_recovered")) %>%
ggplot(aes(x = reorder(Country.Region,Cases_count),
y = Cases_count,
fill = as.factor(Cases_type)
)) +
geom_col() +
scale_y_continuous(labels = scales::comma_format()) +
coord_flip() +
facet_wrap(~continent) +
labs(title = "All type cases of top countries",
subtitle = "Top countries is based on Highest Confirmed cases till date",
caption = "created by ViSa!!")
df_stack %>%
group_by(Country.Region, continent, Date, Cases_type) %>%
summarise(Cases_count = sum(Cases_count, na.rm = TRUE)) %>%
ungroup() %>%
filter(Cases_type == "Active",
Country.Region %in% top15_active) %>%
ggplot(aes(x = reorder(Country.Region,Cases_count),
y = Cases_count ,
fill = as.factor(continent)
)) +
geom_col() +
scale_y_continuous(labels = scales::comma_format()) +
coord_flip()
from: https://www.kaggle.com/janiobachmann/time-series-feature-engineering-concepts
df_stack %>%
group_by(Country.Region, continent, Date, Cases_type) %>%
summarise(Cases_count = sum(Cases_count, na.rm = TRUE)) %>%
ungroup() %>%
filter(Cases_type == "Active",
Country.Region %in% top15_active) %>%
ggplot(aes(x = reorder(Country.Region,Cases_count),
y = Cases_count ,
fill = as.factor(continent)
)) +
geom_col() +
scale_y_continuous(labels = scales::comma_format()) +
scale_fill_tableau() +
geom_label(aes(label = Cases_count), size=3, color="white") +
coord_flip()
from: https://jkunst.com/highcharter/articles/modules.html
set.seed(123)
n <- 200
colors <- sample(viridisLite::cividis(5, end = .9))
df <- tibble(
x = 1:n,
y = abs(arima.sim(n = n, model = list(ar = c(0.9)))) + 2,
y2 = 10 + y,
col = rep(colors, each = n/10, length.out = n)
)
hchart(df, "coloredarea", hcaes(x, y, segmentColor = col)) %>%
hc_add_series(df, "coloredline", hcaes(x, y2 , segmentColor = col)) %>%
hc_add_dependency("plugins/multicolor_series.js")
hchart(df, "coloredline", hcaes(x, y2, segmentColor = col)) %>%
# hc_add_series(df, "coloredline", hcaes(x, y2 , segmentColor = col)) %>%
hc_add_dependency("plugins/multicolor_series.js")
cases in India
df_stack %>%
filter(Country.Region == "India",
Date == max(Date, na.rm = TRUE)) %>%
hchart("funnel",
hcaes(x = Cases_type, y = Cases_count)) %>%
hc_caption(text = "chart created by ViSa :)")
df_all_cases_sum %>% head()
df_all_cases_sum %>%
hchart("scatter",
hcaes(x = pop, y = Confirmed, size = gdpPercap, group = continent),
maxSize = "15%") %>%
hc_caption(text = "chart created by ViSa :)")
df_all_cases_sum %>%
hchart("scatter",
hcaes(x = gdpPercap, y = Confirmed, size = pop, group = continent),
maxSize = "15%") %>%
hc_caption(text = "chart created by ViSa :)")
from: https://jkunst.com/blog/posts/2019-02-04-using-tooltips-in-unexpected-ways/
adding name = Country_Region to add it to tooltip but that doesnt work
df_all_cases_sum %>%
mutate(conf_to_pop = Confirmed / pop * 100) %>%
hchart("scatter",
hcaes(x = gdpPercap, y = conf_to_pop,
size = pop, group = continent, name = Country_Region),
maxSize = "15%") %>%
hc_title(text = "Ratio of Confirmed Cases to population Vs gdpPercap") %>%
hc_caption(text = "chart created by ViSa :)")
from: https://paldhous.github.io/ucb/2016/dataviz/week13.html
yAxis to percent scale: https://www.highcharts.com/blog/tutorials/highcharts-for-r-users/
bubble_plt1 <- df_all_cases_sum %>%
mutate(confirmed_to_pop = round(Confirmed / pop * 100, digits = 2)) %>%
hchart("scatter",
hcaes(x = gdpPercap, y = confirmed_to_pop,
size = pop, group = continent),
maxSize = "15%") %>%
hc_title(text = "Percentage of Covid Confirmed cases to population Vs gdpPercap") %>%
hc_subtitle(text = "Bubble size relative to size of population") %>%
hc_tooltip(shared = TRUE,
# borderColor = "black",
pointFormat = "{point.Country_Region}<br>
Confirmed cases to population: {point.confirmed_to_pop}%<br>
pop: {point.pop}, gdpPercap: {point.gdpPercap} <br>"
) %>%
hc_yAxis(labels = list(format = "{value}%")) %>%
hc_caption(text = "chart created by ViSa :)")
bubble_plt1
to save above plot
from: https://paldhous.github.io/ucb/2016/dataviz/week13.html
library(htmlwidgets)
saveWidget(bubble_plt1, "bubbleplt.html", selfcontained = TRUE, libdir = NULL, background = "white")
bubble_plt2 <- df_all_cases_sum %>%
mutate(confirmed_to_pop = round(Confirmed / pop * 100, digits = 2)) %>%
hchart("scatter",
hcaes(x = gdpPercap, y = confirmed_to_pop,
size = pop, group = continent),
maxSize = "15%") %>%
hc_title(text = "Percentage of Covid Confirmed cases to population Vs gdpPercap") %>%
hc_subtitle(text = "Bubble size relative to size of population") %>%
hc_tooltip(shared = TRUE,
# borderColor = "black",
pointFormat = "{point.Country_Region}<br>
Confirmed cases to population: {point.confirmed_to_pop}%<br>
pop: {point.pop}, gdpPercap: {point.gdpPercap} <br>"
) %>%
hc_yAxis(labels = list(format = "{value}%"),
plotLines = list(
list(
label = list(text = "This is a plotLine"),
color = "grey",
width = 2,
value = 7,
# the zIndex is used to put the label text over the grid lines
zIndex = 1
)
)
) %>%
hc_xAxis(plotLines = list(
list(
label = list(text = "This is a plotLine"),
color = "grey",
width = 2,
value = 90000,
# the zIndex is used to put the label text over the grid lines
zIndex = 4
)
)
) %>%
hc_caption(text = "chart created by ViSa :)")
bubble_plt2
df_all_cases_sum %>%
mutate(confirmed_to_pop = round(Confirmed / pop * 100, digits = 2)) %>%
filter(Country_Region == "Monaco")
from: https://jkunst.com/highcharter/reference/hc_annotations.html
# Ex 1
highchart() %>%
hc_add_series(
data = c(29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4)
) %>%
hc_xAxis(
tickInterval = 0.5,
gridLineWidth = 1
) %>%
hc_annotations(
list(
labels =
list(
list(
point = list(x = 3, y = 129.2, xAxis = 0, yAxis = 0),
text = "x: {x}<br/>y: {y}"
),
list(
point = list(x = 9, y = 194.1, xAxis = 0, yAxis = 0),
text = "x: {x}<br/>y: {y}"
),
list(
point = list(x = 5, y = 100, xAxis = 0),
text = "x: {x}<br/>y: {point.plotY} px"
),
list(
point = list(x = 0, y = 0),
text = "x: {point.plotX} px<br/>y: {point.plotY} px"
)
)
)
)
# Ex 2
df <- data.frame(
x = 1:10,
y = 1:10
)
highchart() %>%
hc_add_series(data = df, hcaes(x = x, y = y), type = "area") %>%
hc_annotations(
list(
labels = list(
list(point = list(x = 5, y = 5, xAxis = 0, yAxis = 0), text = "Middle"),
list(point = list(x = 1, y = 1, xAxis = 0, yAxis = 0), text = "Start")
)
)
)
plot_data <- df_all_cases_sum %>%
mutate(confirmed_to_pop = round(Confirmed / pop * 100, digits = 2),
xmedian = median(gdpPercap, na.rm = TRUE),
ymedian = median(confirmed_to_pop, na.rm = TRUE))
bubble_plt3 <- hchart(plot_data, "scatter",
hcaes(x = gdpPercap, y = confirmed_to_pop,
size = pop, group = continent),
maxSize = "15%") %>%
hc_title(text = "Percentage of Covid Confirmed cases to population Vs gdpPercap") %>%
hc_subtitle(text = "Bubble size relative to size of population") %>%
hc_tooltip(shared = TRUE,
# borderColor = "black",
pointFormat = "{point.Country_Region}<br>
Confirmed cases to population: {point.confirmed_to_pop}%<br>
pop: {point.pop}, gdpPercap: {point.gdpPercap} <br>"
) %>%
hc_yAxis(labels = list(format = "{value}%"),
plotLines = list(
list(
# label = list(text = "This is a plotLine"),
color = "grey",
width = 2,
value = unique(plot_data$ymedian) #,
# the zIndex is used to put the label text over the grid lines
# zIndex = 1
)
)
) %>%
hc_xAxis(plotLines = list(
list(
# label = list(text = "This is a plotLine"),
color = "grey",
width = 2,
value = unique(plot_data$xmedian) #,
# the zIndex is used to put the label text over the grid lines
# zIndex = 4
)
)
) %>%
hc_caption(text = "chart created by ViSa :)") #%>%
# hc_annotations(
# list(
# labels = list(
# list(point = list(x = 140000, y = 5.5, xAxis = 0, yAxis = 0), text = "Best Quadrant"),
# list(point = list(x = 140000, y = 13, xAxis = 0, yAxis = 0), text = "Rich with High cases Quadrant"),
# list(point = list(x = 45000, y = 13, xAxis = 0, yAxis = 0), text = "Worst Quadrant"),
# list(point = list(x = 45000, y = 5.5, xAxis = 0, yAxis = 0), text = "World Quadrant")
# )
# )
# )
bubble_plt3
saveWidget(bubble_plt3, "bubble_annotation_plt.html", selfcontained = TRUE, libdir = NULL, background = "white")
df_all_cases_sum %>%
mutate(confirmed_to_pop = round(Confirmed / pop * 100, digits = 2),
xmedian = mean(gdpPercap, na.rm = TRUE),
ymedian = mean(confirmed_to_pop, na.rm = TRUE))
plot_data <- df_all_cases_sum %>%
mutate(confirmed_to_pop = round(Confirmed / pop * 100, digits = 2),
xmid = max(gdpPercap, na.rm = TRUE)/2,
ymid = max(confirmed_to_pop, na.rm = TRUE)/2)
bubble_plt4 <- hchart(plot_data, "scatter",
hcaes(x = gdpPercap, y = confirmed_to_pop,
size = pop, group = continent),
maxSize = "15%") %>%
hc_title(text = "Percentage of Covid Confirmed cases to population Vs gdpPercap") %>%
hc_subtitle(text = "Bubble size relative to size of population") %>%
hc_tooltip(shared = TRUE,
# borderColor = "black",
pointFormat = "{point.Country_Region}<br>
Confirmed cases to population: {point.confirmed_to_pop}%<br>
pop: {point.pop}, gdpPercap: {point.gdpPercap} <br>"
) %>%
hc_yAxis(labels = list(format = "{value}%"),
plotLines = list(
list(
# label = list(text = "This is a plotLine"),
color = "grey",
width = 2,
value = unique(plot_data$ymid) #,
# the zIndex is used to put the label text over the grid lines
# zIndex = 1
)
)
) %>%
hc_xAxis(plotLines = list(
list(
# label = list(text = "This is a plotLine"),
color = "grey",
width = 2,
value = unique(plot_data$xmid) #,
# the zIndex is used to put the label text over the grid lines
# zIndex = 4
)
)
) %>%
hc_annotations(
list(
labels = list(
list(point = list(x = unique(plot_data$xmid)*1.5, y = 5.5, xAxis = 0, yAxis = 0), text = "Best Quadrant"),
list(point = list(x = unique(plot_data$xmid)*1.5, y = 13, xAxis = 0, yAxis = 0), text = "Rich with High cases Quadrant"),
list(point = list(x = unique(plot_data$xmid)*.5, y = 13, xAxis = 0, yAxis = 0), text = "Worst Quadrant"),
list(point = list(x = unique(plot_data$xmid)*.5, y = 5.5, xAxis = 0, yAxis = 0), text = "World Quadrant")
)
)
) %>%
hc_caption(text = "chart created by ViSa :)")
bubble_plt4
Size based on population
Data/Plot Without Monaco
plot_data <- df_all_cases_sum %>% filter(Country_Region != "Monaco") %>%
mutate(confirmed_to_pop = round(Confirmed / pop * 100, digits = 2),
xmid = max(gdpPercap, na.rm = TRUE)/2,
ymid = max(confirmed_to_pop, na.rm = TRUE)/2)
bubble_plt5 <- hchart(plot_data, "scatter",
hcaes(x = gdpPercap, y = confirmed_to_pop,
size = pop, group = continent),
maxSize = "15%") %>%
hc_title(text = "Percentage of Covid Confirmed cases to population Vs gdpPercap") %>%
hc_subtitle(text = "Bubble size relative to size of population") %>%
hc_tooltip(shared = TRUE,
# borderColor = "black",
pointFormat = "{point.Country_Region}<br>
Confirmed cases to population: {point.confirmed_to_pop}%<br>
pop: {point.pop}, gdpPercap: {point.gdpPercap} <br>"
) %>%
hc_yAxis(labels = list(format = "{value}%"),
plotLines = list(
list(
# label = list(text = "This is a plotLine"),
color = "grey",
width = 2,
value = unique(plot_data$ymid) #,
# the zIndex is used to put the label text over the grid lines
# zIndex = 1
)
)
) %>%
hc_xAxis(plotLines = list(
list(
# label = list(text = "This is a plotLine"),
color = "grey",
width = 2,
value = unique(plot_data$xmid) #,
# the zIndex is used to put the label text over the grid lines
# zIndex = 4
)
)
) %>%
hc_annotations(
list(
labels = list(
list(point = list(x = unique(plot_data$xmid)*1.5, y = 5.5, xAxis = 0, yAxis = 0), text = "Best Quadrant"),
list(point = list(x = unique(plot_data$xmid)*1.5, y = 13, xAxis = 0, yAxis = 0), text = "Rich with High cases Quadrant"),
list(point = list(x = unique(plot_data$xmid)*.5, y = 13, xAxis = 0, yAxis = 0), text = "Worst Quadrant"),
list(point = list(x = unique(plot_data$xmid)*.5, y = 5.5, xAxis = 0, yAxis = 0), text = "World Quadrant")
)
)
) %>%
hc_caption(text = "chart created by ViSa :)")
bubble_plt5
Reversing Quadrants / axis
plot_data <- df_all_cases_sum %>% filter(Country_Region != "Monaco") %>%
mutate(confirmed_to_pop = round(Confirmed / pop * 100, digits = 2),
ymid = max(gdpPercap, na.rm = TRUE)/2,
xmid = max(confirmed_to_pop, na.rm = TRUE)/2)
bubble_plt_reversed_6 <- hchart(plot_data, "scatter",
hcaes(y = gdpPercap, x = confirmed_to_pop,
size = pop, group = continent),
maxSize = "15%") %>%
hc_title(text = "Covid Confirmed cases to population (%) Vs gdpPercap") %>%
hc_subtitle(text = "Bubble size relative to size of population") %>%
hc_tooltip(shared = TRUE,
# borderColor = "black",
pointFormat = "{point.Country_Region}<br>
Confirmed cases to population: {point.confirmed_to_pop}%<br>
pop: {point.pop}, gdpPercap: {point.gdpPercap} <br>"
) %>%
hc_yAxis(
plotLines = list(
list(
# label = list(text = "This is a plotLine"),
color = "grey",
width = 2,
value = unique(plot_data$ymid) #,
# the zIndex is used to put the label text over the grid lines
# zIndex = 1
)
)
) %>%
hc_xAxis(labels = list(format = "{value}%"),
plotLines = list(
list(
# label = list(text = "This is a plotLine"),
color = "grey",
width = 2,
value = unique(plot_data$xmid) #,
# the zIndex is used to put the label text over the grid lines
# zIndex = 4
)
)
) %>%
hc_annotations(
list(
labels = list(
list(point = list(y = unique(plot_data$ymid)*.7, x = 9.5, xAxis = 0, yAxis = 0), text = "Worst Quadrant"),
list(point = list(y = unique(plot_data$ymid)*1.8, x = 9.5, xAxis = 0, yAxis = 0), text = "Rich with High cases Quadrant"),
list(point = list(y = unique(plot_data$ymid)*.7, x = 3, xAxis = 0, yAxis = 0), text = "World Quadrant"),
list(point = list(y = unique(plot_data$ymid)*1.8, x = 3, xAxis = 0, yAxis = 0), text = "Best Quadrant")
)
)
) %>%
hc_caption(text = "chart created by ViSa :)")
bubble_plt_reversed_6
df_all_cases_sum %>%
filter(str_detect(str_to_lower(Country_Region), "vietnam"))
plot_data <- df_all_cases_sum %>% #filter(Country_Region != "Monaco") %>%
mutate(confirmed_to_pop = round(Confirmed / pop * 100, digits = 2),
ymid = max(gdpPercap, na.rm = TRUE)/2,
xmid = max(confirmed_to_pop, na.rm = TRUE)/2)
bubble_plt_reversed_7 <- hchart(plot_data, "scatter",
hcaes(y = gdpPercap, x = confirmed_to_pop,
size = pop, group = continent),
maxSize = "15%") %>%
hc_title(text = "Covid Confirmed cases to population (%) Vs gdpPercap") %>%
hc_subtitle(text = "Bubble size relative to size of population") %>%
hc_tooltip(shared = TRUE,
# borderColor = "black",
pointFormat = "{point.Country_Region}<br>
Confirmed cases to population: {point.confirmed_to_pop}%<br>
pop: {point.pop}, gdpPercap: {point.gdpPercap} <br>"
) %>%
hc_yAxis(
plotLines = list(
list(
# label = list(text = "This is a plotLine"),
color = "grey",
width = 2,
value = unique(plot_data$ymid) #,
# the zIndex is used to put the label text over the grid lines
# zIndex = 1
)
)
) %>%
hc_xAxis(labels = list(format = "{value}%"),
plotLines = list(
list(
# label = list(text = "This is a plotLine"),
color = "grey",
width = 2,
value = unique(plot_data$xmid) #,
# the zIndex is used to put the label text over the grid lines
# zIndex = 4
)
)
) %>%
hc_annotations(
list(
labels = list(
list(point = list(y = unique(plot_data$ymid)*.7, x = 9.5, xAxis = 0, yAxis = 0), text = "Worst Quadrant"),
list(point = list(y = unique(plot_data$ymid)*1.95, x = 9.5, xAxis = 0, yAxis = 0), text = "Rich with High cases Quadrant"),
list(point = list(y = unique(plot_data$ymid)*.7, x = 3, xAxis = 0, yAxis = 0), text = "World Quadrant"),
list(point = list(y = unique(plot_data$ymid)*1.95, x = 3, xAxis = 0, yAxis = 0), text = "Best Quadrant")
)
)
) %>%
hc_caption(text = "chart created by ViSa :)")
bubble_plt_reversed_7
saveWidget(bubble_plt_reversed_7, "bubble_reversed_plt.html", selfcontained = TRUE, libdir = NULL, background = "white")
Size based on (Confirmed cases / population)
plot_data <- df_all_cases_sum %>% #filter(Country_Region != "Monaco") %>%
mutate(confirmed_to_pop = round(Confirmed / pop * 100, digits = 2),
ymid = max(gdpPercap, na.rm = TRUE)/2,
xmid = max(pop, na.rm = TRUE)/2)
bubble_plt_reversed_8 <- hchart(plot_data, "scatter",
hcaes(y = gdpPercap, x = pop,
size = confirmed_to_pop, group = continent),
maxSize = "15%") %>%
hc_title(text = "gdpPercap Vs population ") %>%
hc_subtitle(text = "Bubble size relative to confirmed cases/population ratio") %>%
hc_tooltip(shared = TRUE,
# borderColor = "black",
pointFormat = "{point.Country_Region}<br>
Confirmed cases to population: {point.confirmed_to_pop}%<br>
pop: {point.pop}, gdpPercap: {point.gdpPercap} <br>"
) %>%
hc_yAxis(
plotLines = list(
list(
# label = list(text = "This is a plotLine"),
color = "grey",
width = 2,
value = unique(plot_data$ymid) #,
# the zIndex is used to put the label text over the grid lines
# zIndex = 1
)
)
) %>%
hc_xAxis(#labels = list(format = "{value}%"),
plotLines = list(
list(
# label = list(text = "This is a plotLine"),
color = "grey",
width = 2,
value = unique(plot_data$xmid) #,
# the zIndex is used to put the label text over the grid lines
# zIndex = 4
)
)
) %>%
hc_annotations(
list(
labels = list(
list(point = list(y = unique(plot_data$ymid)*.7, x = unique(plot_data$xmid)*1.5,
xAxis = 0, yAxis = 0), text = "High Pop with low gdpPercap Quadrant"),
list(point = list(y = unique(plot_data$ymid)*2.5, x = unique(plot_data$xmid)*.5,
xAxis = 0, yAxis = 0), text = "Rich with High gdpPercap Quadrant"),
list(point = list(y = unique(plot_data$ymid)*.7, x = unique(plot_data$xmid)*.5,
xAxis = 0, yAxis = 0), text = "World Quadrant"),
list(point = list(y = unique(plot_data$ymid)*2.5, x = unique(plot_data$xmid)*1.5,
xAxis = 0, yAxis = 0), text = "High Pop & gdpPercap")
)
)
) %>%
hc_caption(text = "chart created by ViSa :)")
bubble_plt_reversed_8
saveWidget(bubble_plt_reversed_8, "bubble_gdpVspop_plt.html", selfcontained = TRUE, libdir = NULL, background = "white")
plot_data <- df_all_cases_sum %>% filter(!Country_Region %in% c("Monaco","Luxembourg")) %>%
mutate(confirmed_to_pop = round(Confirmed / pop * 100, digits = 2),
ymid = max(gdpPercap, na.rm = TRUE)/2,
xmid = max(pop, na.rm = TRUE)/2)
bubble_plt_reversed_9 <- hchart(plot_data, "scatter",
hcaes(y = gdpPercap, x = pop,
size = confirmed_to_pop, group = continent),
maxSize = "15%") %>%
hc_title(text = "gdpPercap Vs population ") %>%
hc_subtitle(text = "Bubble size relative to confirmed cases/population ratio") %>%
hc_tooltip(shared = TRUE,
# borderColor = "black",
pointFormat = "{point.Country_Region}<br>
Confirmed cases to population: {point.confirmed_to_pop}%<br>
pop: {point.pop}, gdpPercap: {point.gdpPercap} <br>"
) %>%
hc_yAxis(
plotLines = list(
list(
# label = list(text = "This is a plotLine"),
color = "grey",
width = 2,
value = unique(plot_data$ymid) #,
# the zIndex is used to put the label text over the grid lines
# zIndex = 1
)
)
) %>%
hc_xAxis(#labels = list(format = "{value}%"),
plotLines = list(
list(
# label = list(text = "This is a plotLine"),
color = "grey",
width = 2,
value = unique(plot_data$xmid) #,
# the zIndex is used to put the label text over the grid lines
# zIndex = 4
)
)
) %>%
hc_annotations(
list(
labels = list(
list(point = list(y = unique(plot_data$ymid)*.7, x = unique(plot_data$xmid)*1.5,
xAxis = 0, yAxis = 0), text = "High Pop with low gdpPercap Quadrant"),
list(point = list(y = unique(plot_data$ymid)*2, x = unique(plot_data$xmid)*.5,
xAxis = 0, yAxis = 0), text = "Rich with High gdpPercap Quadrant"),
list(point = list(y = unique(plot_data$ymid)*.7, x = unique(plot_data$xmid)*.5,
xAxis = 0, yAxis = 0), text = "World Quadrant"),
list(point = list(y = unique(plot_data$ymid)*2, x = unique(plot_data$xmid)*1.5,
xAxis = 0, yAxis = 0), text = "High Pop & gdpPercap")
)
)
) %>%
hc_caption(text = "chart created by ViSa :)")
bubble_plt_reversed_9
from: https://jkunst.com/highcharter/reference/data_to_hierarchical.html
df_all_cases_sum %>%
filter(Country_Region %in% top_20) %>%
highcharter::data_to_hierarchical(c(continent, Country_Region), Confirmed) %>%
hchart(type = "sunburst")
library(viridisLite)
cols_virdis <- viridis(4)
cols_virdis <- substr(cols_virdis, 0, 7)
cols_virdis
df_all_cases_sum %>%
filter(Country_Region %in% top_20) %>%
highcharter::data_to_hierarchical(
group_vars = c(continent, Country_Region),
size_var = Confirmed,
# colors = getOption("hicharter.color_palette")
colors = cols_virdis) %>%
hchart(type = "sunburst")
from: https://www.datanovia.com/en/blog/the-a-z-of-rcolorbrewer-palette/
library(RColorBrewer)
display.brewer.all()
col_selected <- RColorBrewer::display.brewer.all(n = 4, select = "Set3")
col_selected
display.brewer.pal(n = 4, name = "Set2")
col_selected <- brewer.pal(n =4, name = "Set2")
col_selected
df_all_cases_sum %>%
filter(Country_Region %in% top_20) %>%
highcharter::data_to_hierarchical(
group_vars = c(continent, Country_Region),
size_var = Confirmed,
# colors = getOption("hicharter.color_palette")
colors = col_selected) %>%
hchart(type = "sunburst")
df_all_cases_sum %>%
filter(Country_Region %in% top_20) %>%
mutate(world = "World top 20 confirmed cases") %>%
highcharter::data_to_hierarchical(
group_vars = c(world, continent, Country_Region),
size_var = Confirmed,
# colors = getOption("hicharter.color_palette")
colors = col_selected) %>%
hchart(type = "sunburst")
df_all_cases_sum %>%
filter(Country_Region %in% top_20) %>%
highcharter::data_to_hierarchical(
group_vars = c(continent, Country_Region),
size_var = Confirmed,
# colors = getOption("hicharter.color_palette")
colors = col_selected) %>%
hchart(type = "treemap")
df_all_cases_sum %>%
filter(Country_Region %in% top_10) %>%
highcharter::data_to_hierarchical(
group_vars = c(continent, Country_Region),
size_var = Confirmed,
# colors = getOption("hicharter.color_palette")
colors = col_selected) %>%
hchart(type = "sunburst")
df_all_cases_sum %>%
filter(Country_Region == top_20) %>%
plot_ly(data = .,
labels = .$Country_Region,
parents = .$continent,
values = .$Confirmed,
type = "sunburst"
)
from: https://www.kaggle.com/anshumoudgil/untying-climate-s-knots-visualisation
df_all_cases_sum %>% head()
df_all_cases_sum %>%
filter(Country_Region %in% top_10) %>%
ggplot(aes(x = Country_Region, y = continent)) +
geom_point(aes(size = Confirmed), shape = 16, color = "red")
df_all_cases_sum %>%
filter(Country_Region %in% top_10) %>%
ggplot(aes(x = Country_Region, y = continent)) +
geom_point(size = 5, shape = 19, color = "red") +
geom_point(size = 2, shape = 16, color = "white") +
theme_minimal()
df_stack %>% head()
df_stack %>%
select(Country.Region, continent) %>%
unique()
df_stack %>%
mutate(month = month(Date)) %>%
filter(Cases_type == "Confirmed") %>%
group_by(month, Country.Region) %>%
summarise(Cases_count = sum(Cases_count, na.rm = TRUE)) %>%
top_n(n = 10, wt = Cases_count)
df_stack %>%
mutate(month = month(Date)) %>%
filter(Cases_type == "Confirmed") %>%
group_by(month, Country.Region) %>%
summarise(Cases_count = sum(Cases_count, na.rm = TRUE)) %>%
top_n(n = 10, wt = Cases_count) %>%
ungroup() %>%
left_join(y = df_stack %>%
select(Country.Region, continent) %>%
unique(),
by = "Country.Region")
df_stack %>%
mutate(month = month(Date)) %>%
filter(Cases_type == "Confirmed") %>%
group_by(month, Country.Region) %>%
summarise(Cases_count = sum(Cases_count, na.rm = TRUE)) %>%
top_n(n = 5, wt = Cases_count) %>%
ungroup() %>%
left_join(y = df_stack %>%
select(Country.Region, continent) %>%
unique(),
by = "Country.Region") %>%
ggplot(aes(x = Country.Region, y = continent)) +
geom_point(size = 6, shape = 19, color = "midnightblue") +
geom_point(size = 2, shape = 16, color = "white") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90)) +
facet_wrap(~month) +
labs(title = "Top 5 confirmed cases Countries across the year")
df_stack %>%
mutate(month = month(Date)) %>%
filter(Cases_type == "Confirmed") %>%
group_by(month, Country.Region) %>%
summarise(Cases_count = sum(Cases_count, na.rm = TRUE)) %>%
top_n(n = 5, wt = Cases_count) %>%
ungroup() %>%
left_join(y = df_stack %>%
select(Country.Region, continent) %>%
unique(),
by = "Country.Region") %>%
ggplot(aes(x = Country.Region, y = continent)) +
geom_point(size = 3, shape = 21, color = "midnightblue", fill = "white", stroke = 3) +
# geom_point(size = 2, shape = 16, color = "white") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90)) +
facet_wrap(~month) +
labs(title = "Top 5 confirmed cases Countries across the year")
df_stack %>%
# mutate(month = lubridate::month(Date, label = TRUE, abbr = TRUE)) %>%
mutate(month = format(Date, "%Y-%m")) %>%
filter(Cases_type == "Confirmed") %>%
group_by(month, Country.Region) %>%
summarise(Cases_count = sum(Cases_count, na.rm = TRUE)) %>%
top_n(n = 5, wt = Cases_count) %>%
ungroup() %>%
left_join(y = df_stack %>%
select(Country.Region, continent) %>%
unique(),
by = "Country.Region") %>%
ggplot(aes(x = Country.Region, y = continent)) +
geom_point(size = 3, shape = 21, aes(color = as.factor(month)), fill = "white", stroke = 3) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90),
legend.position = "none") +
facet_wrap(~month) +
labs(title = "Top 5 confirmed cases Countries across the year")
df_stack %>%
# mutate(month = lubridate::month(Date, label = TRUE, abbr = TRUE)) %>%
mutate(month = format(Date, "%Y-%m")) %>%
filter(Cases_type == "Confirmed") %>%
group_by(month, Country.Region) %>%
summarise(Cases_count = sum(Cases_count, na.rm = TRUE)) %>%
top_n(n = 5, wt = Cases_count) %>%
ungroup() %>%
left_join(y = df_stack %>%
select(Country.Region, continent) %>%
unique(),
by = "Country.Region") %>%
ggplot(aes(x = Country.Region, y = continent)) +
geom_point(size = 3, shape = 21, aes(color = as.factor(continent)), fill = "white", stroke = 3) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90),
legend.position = "none") +
facet_wrap(~month) +
labs(title = "Top 5 confirmed cases Countries across the year")
df_stack %>%
# mutate(month = lubridate::month(Date, label = TRUE, abbr = TRUE)) %>%
mutate(month = format(Date, "%Y-%m")) %>%
filter(Cases_type == "Confirmed") %>%
group_by(month, Country.Region) %>%
summarise(Cases_count = sum(Cases_count, na.rm = TRUE)) %>%
top_n(n = 5, wt = Cases_count) %>%
ungroup() %>%
left_join(y = df_stack %>%
select(Country.Region, continent) %>%
unique(),
by = "Country.Region") %>%
ggplot(aes(x = continent, y = Country.Region)) +
geom_point(size = 3, shape = 21, aes(color = as.factor(month)), fill = "white", stroke = 3) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90),
legend.position = "none") +
facet_wrap(~month) +
labs(title = "Top 5 confirmed cases Countries across the year")
df_stack %>%
# mutate(month = lubridate::month(Date, label = TRUE, abbr = TRUE)) %>%
mutate(month = format(Date, "%Y-%m")) %>%
filter(Cases_type == "Confirmed") %>%
group_by(month, Country.Region) %>%
summarise(Cases_count = sum(Cases_count, na.rm = TRUE)) %>%
top_n(n = 5, wt = Cases_count) %>%
ungroup() %>%
left_join(y = df_stack %>%
select(Country.Region, continent) %>%
unique(),
by = "Country.Region") %>%
ggplot(aes(x = continent, y = Country.Region)) +
geom_point(size = 3, shape = 21, aes(color = as.factor(continent)), fill = "white", stroke = 3) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90),
legend.position = "none") +
facet_wrap(~month) +
labs(title = "Top 5 confirmed cases Countries across the year")
size varies based on whole data because of ungroup
df_stack %>%
# mutate(month = lubridate::month(Date, label = TRUE, abbr = TRUE)) %>%
mutate(month = format(Date, "%Y-%m")) %>%
filter(Cases_type == "Confirmed") %>%
group_by(month, Country.Region) %>%
summarise(Cases_count = sum(Cases_count, na.rm = TRUE)) %>%
top_n(n = 5, wt = Cases_count) %>%
ungroup() %>%
left_join(y = df_stack %>%
select(Country.Region, continent) %>%
unique(),
by = "Country.Region") %>%
ggplot(aes(x = continent, y = Country.Region)) +
geom_point(shape = 21, aes(size = Cases_count, color = as.factor(continent)), fill = "white", stroke = 3) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90),
legend.position = "none") +
facet_wrap(~month) +
labs(title = "Top 5 confirmed cases Countries across the year")
size varies based on grouped monthly data
Solution from: https://stackoverflow.com/questions/65456595/how-to-group-or-nest-to-a-subset-of-the-data-in-r/65482285#65482285
df_stack %>%
# mutate(month = lubridate::month(Date, label = TRUE, abbr = TRUE)) %>%
mutate(month = format(Date, "%Y-%m")) %>%
filter(Cases_type == "Confirmed") %>%
group_by(month, Country.Region) %>%
dplyr::summarise(Cases_count = sum(Cases_count, na.rm = TRUE),
continent = first(continent)) %>%
top_n(n = 5, wt = Cases_count) %>%
group_by(month) %>%
mutate(Cases_size = Cases_count / sum(Cases_count)) %>%
ggplot(aes(x = continent, y = Country.Region)) +
geom_point(shape = 21, aes(size = Cases_size, color = as.factor(continent)), fill = "white", stroke = 3) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90),
legend.position = "none") +
facet_wrap(~month) +
coord_cartesian(clip = "off") +
scale_color_tableau() +
labs(title = "Top 5 confirmed cases Countries based on each month")
df_stack %>%
mutate(month_yr = paste0(lubridate::month(Date, label = TRUE, abbr = TRUE),"_",lubridate::year(Date)
# ),
#
# # to set order of month factors
# month_yr = as.factor(month_yr, levels = c("Jan_2020","Feb_2020","Mar_2020",
# "Apr_2020","May_2020","Jun_2020",
# "Jul_2020","Aug_2020","Sep_2020",
# "Oct_2020","Nov_2020","Dec_2020",
# "Jan_2021")
)) %>%
filter(Cases_type == "Confirmed") %>%
group_by(month_yr, Country.Region) %>%
summarise(Cases_count = sum(Cases_count, na.rm = TRUE),
continent = first(continent)) %>%
top_n(n = 5, wt = Cases_count) %>%
group_by(month_yr) %>% #pull(month_yr) %>% unique()
mutate(Cases_size = Cases_count / sum(Cases_count)) %>%
ggplot(aes(x = continent, y = Country.Region)) +
geom_point(shape = 21, # alpha = 0.6,
aes(size = Cases_size, color = as.factor(continent)), fill = "white", stroke = 3) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90),
legend.position = "none") +
facet_wrap(~month_yr) +
coord_cartesian(clip = "off") +
scale_color_tableau() +
labs(title = "Top 5 confirmed cases Countries based on each month")
to handle issue of month to month_yr:
df_stack %>%
mutate(month_yr = format(Date, "%Y-%m")) %>%
filter(Cases_type == "Confirmed") %>%
group_by(month_yr, Country.Region) %>%
summarise(Cases_count = sum(Cases_count, na.rm = TRUE),
continent = first(continent)) %>%
top_n(n = 5, wt = Cases_count) %>%
group_by(month_yr) %>% #pull(month_yr) %>% unique()
mutate(Cases_size = Cases_count / sum(Cases_count)) %>%
ggplot(aes(x = continent, y = Country.Region)) +
geom_point(shape = 21, # alpha = 0.6,
aes(size = Cases_size, color = as.factor(continent)), fill = "white", stroke = 3) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90),
legend.position = "none") +
facet_wrap(~month_yr) +
coord_cartesian(clip = "off") +
scale_color_tableau() +
labs(title = "Top 5 confirmed cases Countries based on each month")
https://prsindia.org/covid-19/cases
cases_ts_India <- df_stack %>%
filter(Country.Region == "India") %>%
ggplot(aes(x = Date, y = Cases_count, col = Cases_type)) +
geom_path()
cases_ts_India
ggplotly(cases_ts_India)
ggplotly(cases_ts_India) %>%
layout(legend = list(orientation = "h", y =-0.2))
from: https://stackoverflow.com/questions/40001518/add-title-to-the-plotly-legend https://stackoverflow.com/questions/65466286/how-to-adjust-legend-title-position-in-ggplotly-in-r?noredirect=1#comment115742429_65466286
cases_ts_India <- cases_ts_India +
theme( legend.title = element_blank())
legendtitle <- list(yref='paper',xref="paper",y=-.28, x=0.06, text="Cases_type",showarrow=F)
ggplotly(cases_ts_India) %>%
layout(title = "India Cases types over the year ",
legend = list(orientation = "h", x =0.2, y = -0.2),
annotations = legendtitle)
rates_ind_plot <- grouped_cases_df %>%
filter(Country.Region == "India") %>%
select(Country.Region, Date, Case_Fatality_Ratio, Recovery_Rate) %>%
gather(key = "Ratio_type", value = Ratio_value, -c(Country.Region, Date)) %>%
ggplot(aes(x = Date, y = Ratio_value, col = Ratio_type)) +
geom_path(size = 1.2)
rates_ind_plot
rates_ind_plot <- rates_ind_plot + theme( legend.title = element_blank())
legendtitle <- list(yref='paper',xref="paper",y=-.28, x=0.06, text="Ratio_type",showarrow=F)
ggplotly(rates_ind_plot) %>%
layout(title = "India Cases rates over the year ",
legend = list(orientation = "h", x =0.2, y = -0.2),
annotations = legendtitle)
df_all_cases_sum %>%
hchart(type = "treemap",
hcaes(x = Country_Region, value = Confirmed, color = Confirmed)) %>%
hc_colorAxis(stops = color_stops(colors = viridis::inferno(10))) %>%
hc_title(text = "Relative Size of Cumulative confirmed Cases of countries")
df_all_cases_sum %>%
hchart(type = "treemap",
hcaes(x = Country_Region, value = Active, color = Active)) %>%
hc_colorAxis(stops = color_stops(colors = viridis::inferno(10))) %>%
hc_title(text = "Relative Size of Current Active Cases of countries")
grouped_cases_df %>%
filter(Country.Region %in% (df_all_cases_sum %>%
top_n(n = 15, wt = Active) %>%
pull(Country_Region))
) %>%
ggplot(aes(x = Date, y = Active, col = continent)) +
geom_path() +
scale_y_continuous(labels = scales::comma_format()) +
scale_color_tableau()
# geom_label(aes(label = Active), size=3, color="white")
top15_Active_plt <- grouped_cases_df %>%
filter(Country.Region != "US",
Country.Region %in% (df_all_cases_sum %>%
top_n(n = 15, wt = Active) %>%
pull(Country_Region))
) %>%
ggplot(aes(x = Date, y = Active, col = Country.Region)) +
geom_line() +
scale_y_continuous(labels = scales::comma_format()) +
scale_color_tableau(palette = "Tableau 20") +
facet_wrap(~continent) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90)) +
labs(title = "Top 15 Countries(-US) with Active Cases",
caption = "created by ViSa!!")
top15_Active_plt
ggplotly(top15_Active_plt) %>%
layout(legend = list(orientation = "h", y = -0.2))
grouped_cases_df %>%
filter(Country.Region != "US",
Country.Region %in% (df_all_cases_sum %>%
top_n(n = 15, wt = Active) %>%
pull(Country_Region))
) %>%
rename(Country_Region = Country.Region) %>%
hchart(type = "line",
hcaes(x = Date, y = Active, group = Country_Region)) %>%
hc_tooltip(
# shared = TRUE,
pointFormat = "{point.Country_Region}, {point.Active} <br>"
# CFR Level: {point.CFR_level}, {point.Case_Fatality_Ratio} <br>
) %>%
hc_title(text = "Top 15 Covid Active cases countries") %>%
hc_add_theme(hc_theme_monokai()) %>%
# hc_yAxis(type = "logarithmic") %>%
hc_caption(text = "chart created by ViSa :)")
grouped_cases_df %>%
filter(Country.Region != "US",
Country.Region %in% (df_all_cases_sum %>%
top_n(n = 15, wt = Active) %>%
pull(Country_Region))
) %>%
rename(Country_Region = Country.Region) %>%
hchart(type = "line",
hcaes(x = Date, y = Active, group = Country_Region)) %>%
hc_tooltip(
# shared = TRUE,
pointFormat = "{point.Country_Region}, {point.Active} <br>"
# CFR Level: {point.CFR_level}, {point.Case_Fatality_Ratio} <br>
) %>%
hc_title(text = "Log of Top 15 Covid Active cases countries") %>%
hc_add_theme(hc_theme_monokai()) %>%
hc_yAxis(type = "logarithmic") %>%
hc_caption(text = "chart created by ViSa :)")
grouped_cases_df %>%
filter(Country.Region != "US",
Country.Region %in% (df_all_cases_sum %>%
top_n(n = 15, wt = Confirmed) %>%
pull(Country_Region))
) %>%
rename(Country_Region = Country.Region) %>%
hchart(type = "line",
hcaes(x = Date, y = Active, group = Country_Region)) %>%
hc_tooltip(
# shared = TRUE,
pointFormat = "{point.Country_Region}, {point.Active} <br>"
# CFR Level: {point.CFR_level}, {point.Case_Fatality_Ratio} <br>
) %>%
hc_title(text = "Active cases growth of Top 15 Covid Confirmed countries") %>%
hc_add_theme(hc_theme_monokai()) %>%
# hc_yAxis(type = "logarithmic") %>%
hc_caption(text = "chart created by ViSa :)")
grouped_cases_df %>%
filter(!Country.Region %in% c("US","Turkey"),
Country.Region %in% (grouped_cases_df %>%
filter(Date == max(Date, na.rm = TRUE)) %>%
slice_max(order_by = Daily_cases, n = 15) %>%
pull(Country.Region))
) %>%
rename(Country_Region = Country.Region) %>%
hchart(type = "line",
hcaes(x = Date, y = Daily_cases, group = Country_Region)) %>%
hc_tooltip(
# shared = TRUE,
pointFormat = "{point.Country_Region}, {point.Daily_cases} <br>"
# CFR Level: {point.CFR_level}, {point.Case_Fatality_Ratio} <br>
) %>%
hc_title(text = "Top 15 Covid Daily cases countries as of latest date") %>%
hc_caption(text = "chart created by ViSa!!") %>%
hc_add_theme(hc_theme_monokai())
# hc_yAxis(type = "logarithmic") %>%
grouped_cases_df %>%
filter(!Country.Region %in% c("US","Turkey"),
Country.Region %in% (grouped_cases_df %>%
filter(Date == max(Date, na.rm = TRUE)) %>%
slice_max(order_by = Daily_cases, n = 15) %>%
pull(Country.Region))
) %>%
mutate(CFR_col = case_when(CFR_level == "Low" ~ "#DAF7A6",
CFR_level == "Med" ~ "#4f86b1",
TRUE ~ "#FFC300")
) %>%
rename(Country_Region = Country.Region) %>%
hchart(type = "coloredline",
hcaes(x = Date, y = Daily_cases, group = Country_Region, segmentColor = CFR_col)) %>%
hc_add_dependency("plugins/multicolor_series.js") %>%
hc_tooltip(
shared = TRUE,
pointFormat = "{point.Country_Region}, CFR Level: {point.CFR_level},
{point.Case_Fatality_Ratio},Daily_case: {point.Daily_cases} <br>"
) %>%
hc_title(text = "Top 15 Covid Daily cases cases countries") %>%
hc_subtitle(text = "Colored on basis of CFR level") %>%
hc_caption(text = "chart created by ViSa!!") %>%
hc_add_theme(hc_theme_monokai())
# hc_yAxis(type = "logarithmic") %>%
grouped_cases_df %>%
filter(!Country.Region %in% c("US","Turkey")) %>%
rename(Country_Region = Country.Region) %>%
ggplot(aes(x = Date, y = Daily_cases, col = Country_Region)) +
geom_line(size = 0.8) +
scale_color_tableau(palette = "Tableau 10") +
theme_minimal() +
gghighlight(Country_Region %in% (grouped_cases_df %>%
filter(Date == max(Date, na.rm = TRUE)) %>%
slice_max(order_by = Daily_cases, n = 5) %>%
pull(Country.Region))) +
labs(title = "Top 5 Covid Daily cases cases countries (-US,Turkey)",
subtitle = "Highlighted for Top Countries",
caption = "created by ViSa !!")
grouped_cases_df %>%
filter(!Country.Region %in% c("US","Turkey"),
Country.Region %in% (grouped_cases_df %>%
filter(Date == max(Date, na.rm = TRUE)) %>%
arrange(desc(Daily_cases)) %>%
head(n = 15) %>%
pull(Country.Region))
) %>%
ggplot(aes(x = Date, y = Daily_cases, col = Country.Region)) +
geom_line() +
scale_y_continuous(labels = scales::comma_format()) +
scale_color_tableau(palette = "Tableau 20") +
facet_wrap(~continent) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90)) +
labs(title = "Top 15 Countries(-US) with Daily Cases",
caption = "created by ViSa!!")
ggplotly(grouped_cases_df %>%
filter(!Country.Region %in% c("US","Turkey"),
Country.Region %in% (grouped_cases_df %>%
filter(Date == max(Date, na.rm = TRUE)) %>%
arrange(desc(Daily_cases)) %>%
head(n = 15) %>%
pull(Country.Region))
) %>%
ggplot(aes(x = Date, y = Daily_cases, col = Country.Region)) +
geom_line() +
scale_y_continuous(labels = scales::comma_format()) +
scale_color_tableau(palette = "Tableau 20") +
facet_wrap(~continent) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90)) +
labs(title = "Top 15 Countries(-US) with Daily Cases",
caption = "created by ViSa!!")
) %>%
layout(legend = list(orientation = "h", y = -0.2))
ggplotly(grouped_cases_df %>%
filter(!Country.Region %in% c("US","Turkey"),
Country.Region %in% (grouped_cases_df %>%
filter(Date == max(Date, na.rm = TRUE)) %>%
arrange(desc(Daily_cases)) %>%
head(n = 15) %>%
pull(Country.Region))
) %>%
ggplot(aes(x = Date, y = Daily_cases, col = Country.Region)) +
geom_line() +
scale_y_continuous(labels = scales::comma_format()) +
scale_color_tableau(palette = "Tableau 20") +
# facet_wrap(~continent) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90)) +
labs(title = "Top 15 Countries(-US) with Daily Cases",
caption = "created by ViSa!!")
) %>%
layout(legend = list(orientation = "h", y = -0.2))
ggplotly(
grouped_cases_df %>%
filter(!Country.Region %in% c("US","Turkey"),
Country.Region %in% (grouped_cases_df %>%
filter(Date == max(Date, na.rm = TRUE),
continent == "Europe") %>%
slice_max(order_by = Daily_cases, n = 10) %>%
pull(Country.Region))
) %>%
ggplot(aes(x = Date, y = Daily_cases, col = Country.Region)) +
geom_line() +
scale_y_continuous(labels = scales::comma_format()) +
scale_color_tableau(palette = "Tableau 20") +
# facet_wrap(~continent) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90)) +
labs(title = "Top 10 Countries from Europe(-Turkey) with Daily Cases",
caption = "created by ViSa!!")
)%>%
layout(legend = list(orientation = "h", y = -0.2))
grouped_cases_df %>%
filter(!Country.Region %in% c("US","Turkey"),
Country.Region %in% (grouped_cases_df %>%
filter(Date == max(Date, na.rm = TRUE),
continent == "Europe") %>%
slice_max(order_by = Daily_cases, n = 10) %>%
pull(Country.Region))
) %>%
streamgraph(date = "Date", value = "Daily_cases", key = "Country.Region") %>%
sg_fill_tableau() %>%
sg_title(title = "Top 10 Countries from Europe(-Turkey) with Daily Cases")
grouped_cases_df %>%
filter(Date > as.Date("2020-02-29")) %>%
mutate(month = format(Date, "%Y-%m")) %>%
group_by(Country.Region, month) %>%
summarise(Daily_cases = sum(Daily_cases, na.rm = TRUE)) %>%
group_by(month) %>%
slice_max(order_by = Daily_cases, n = 5) %>%
ungroup() %>%
mutate(Country.Region = fct_reorder(Country.Region, Daily_cases, max)) %>%
# filter(Daily_cases > 2000000)
ggplot(aes(x = Daily_cases, y = Country.Region,
fill = Country.Region, group = Country.Region )) +
geom_col() +
# geom_label(aes(label = Daily_cases), size=2.5, color="white") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90),
legend.position = "none") +
facet_wrap(~month) +
# coord_cartesian(clip = "off") +
scale_fill_tableau(palette = "Tableau 20") +
labs(title = "Top 5 Daily cases countries each month",
subtitle = "Top 5 based on Country's monthly sum of Daily cases in each month",
caption = "Created by ViSa !!") +
scale_x_continuous(labels = unit_format(unit = "M", scale = 1e-6))
Adjust ggplotly() size from: https://stackoverflow.com/questions/37241985/resize-plotly-r-ggplotly
ggplotly(grouped_cases_df %>%
filter(Date > as.Date("2020-02-29")) %>%
mutate(month = format(Date, "%Y-%m")) %>%
group_by(Country.Region, month) %>%
summarise(Daily_cases = sum(Daily_cases, na.rm = TRUE)) %>%
group_by(month) %>%
slice_max(order_by = Daily_cases, n = 5) %>%
ungroup() %>%
mutate(Country.Region = fct_reorder(Country.Region, Daily_cases, max)) %>%
# filter(Daily_cases > 2000000)
ggplot(aes(x = Daily_cases, y = Country.Region,
fill = Country.Region, group = Country.Region )) +
geom_col() +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90),
legend.position = "none") +
facet_wrap(~month) +
# coord_cartesian(clip = "off") +
scale_fill_tableau(palette = "Tableau 20") +
labs(title = "Top 5 Daily cases countries each month",
subtitle = "Top 5 based on Country's monthly sum of Daily cases in each month",
caption = "Created by ViSa !!") +
scale_x_continuous(labels = unit_format(unit = "M", scale = 1e-6)),
height = 1200, width=800)
grouped_cases_df %>%
filter(Country.Region %in% (grouped_cases_df %>%
filter(Date == max(Date, na.rm = TRUE)) %>%
slice_max(Daily_cases, n=10) %>%
pull(Country.Region)
)) %>%
ggplot(aes(x = Date, y = Daily_cases,
col = Country.Region )) +
geom_line(size = 0.8) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90),
legend.position = "none") +
facet_wrap(~Country.Region) +
# coord_cartesian(clip = "off") +
# scale_fill_tableau(palette = "Tableau 20") +
labs(title = "Daily cases growth of Top 10 countries",
subtitle = glue("Top 10 countries displayed is from Highest Daily cases as of {max(grouped_cases_df$Date, na.rm = TRUE)}"),
caption = "Created by ViSa !!") +
scale_y_continuous(labels = unit_format(unit = "k", scale = 1e-3))
ggplotly(grouped_cases_df %>%
filter(Country.Region %in% (grouped_cases_df %>%
filter(Date == max(Date, na.rm = TRUE)) %>%
slice_max(Daily_cases, n=10) %>%
pull(Country.Region)
)) %>%
ggplot(aes(x = Date, y = Daily_cases,
col = Country.Region )) +
geom_line(size = 0.8) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90),
legend.position = "none") +
facet_wrap(~Country.Region) +
# coord_cartesian(clip = "off") +
# scale_fill_tableau(palette = "Tableau 20") +
labs(title = "Daily cases growth of Top 10 countries",
subtitle = glue("Top 10 countries displayed is from Highest Daily cases as of {max(grouped_cases_df$Date, na.rm = TRUE)}"),
caption = "Created by ViSa !!") +
scale_y_continuous(labels = unit_format(unit = "k", scale = 1e-3))
, height = 800, width = 1000)
grouped_cases_df %>%
filter(Country.Region %in% (grouped_cases_df %>%
filter(Date == max(Date, na.rm = TRUE)) %>%
slice_max(Daily_deaths, n=10) %>%
pull(Country.Region)
)) %>%
ggplot(aes(x = Date, y = Daily_deaths,
col = Country.Region )) +
geom_line(size = 0.8) +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90),
legend.position = "none") +
facet_wrap(~Country.Region) +
# coord_cartesian(clip = "off") +
# scale_fill_tableau(palette = "Tableau 20") +
labs(title = "Daily deaths growth of Top 10 countries",
subtitle = glue("Top 10 countries displayed is from Highest Daily deaths as of {max(grouped_cases_df$Date, na.rm = TRUE)}"),
caption = "Created by ViSa !!") +
scale_y_continuous(labels = unit_format(unit = "k", scale = 1e-3))
grouped_cases_df %>%
filter(Date > as.Date("2020-02-29")) %>%
mutate(month = format(Date, "%Y-%m")) %>%
group_by(Country.Region, month) %>%
summarise(Daily_deaths = sum(Daily_deaths, na.rm = TRUE)) %>%
group_by(month) %>%
slice_max(order_by = Daily_deaths, n = 5) %>%
ungroup() %>%
mutate(Country.Region = fct_reorder(Country.Region, Daily_deaths, max)) %>%
ggplot(aes(x = Daily_deaths, y = Country.Region,
fill = Country.Region, group = Country.Region )) +
geom_col() +
# geom_label(aes(label = Daily_cases), size=2.5, color="white") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 90),
legend.position = "none") +
facet_wrap(~month) +
# coord_cartesian(clip = "off") +
scale_fill_tableau(palette = "Tableau 20") +
labs(title = "Top 5 Countries with highest deaths in each month",
subtitle = "Top 5 based on Country's monthly sum of Daily deaths in each month",
caption = "Created by ViSa !!") +
scale_x_continuous(labels = unit_format(unit = "k", scale = 1e-3))
tt_base <- grouped_cases_df %>%
arrange(desc(Date)) %>%
distinct(Country.Region, .keep_all = TRUE)
tt_base
tt_inner <- grouped_cases_df %>%
select(Country.Region, Date, Daily_cases) %>%
mutate(Date = as.integer(Date)) %>%
nest(-Country.Region) %>%
mutate(
data = map(data, mutate_mapping, hcaes(x = Date, y = Daily_cases), drop = TRUE),
data = map(data, list_parse)
) %>%
rename(tt_nestdata = data)
tt_inner
tt_daily <- left_join(tt_base, tt_inner, by = "Country.Region")
tt_daily
tt_plt1 <- hchart(
tt_daily,
"point",
hcaes(x = Confirmed, y = Daily_cases, name = Country.Region,
size = Deaths, group = continent, name = Country.Region)
) %>%
# hc_yAxis(type = "logarithmic") %>%
hc_tooltip(
useHTML = TRUE,
headerFormat = "<b>{point.key}</b>",
pointFormatter = tooltip_chart(accesor = "tt_nestdata")
) %>%
hc_title(text = "Confirmed Vs Daily Cases as of latest Date") %>%
hc_subtitle(text = "Size of bubble based on Deaths <br> (hover over Bubble to view ttchart: Daily cases growth)") %>%
hc_caption(text = "created by ViSa!!")
tt_plt1
saveWidget(tt_plt1, "tt_world_cases.html", selfcontained = TRUE, libdir = NULL, background = "white")
saveWidget(tt_plt1, "tt_world_cases2.html", selfcontained = TRUE, libdir = NULL, background = "#FFEADB")
tt_base <- grouped_cases_df %>%
filter(Country.Region != "US") %>%
arrange(desc(Date)) %>%
distinct(Country.Region, .keep_all = TRUE)
tt_base
tt_inner <- grouped_cases_df %>%
filter(Country.Region != "US") %>%
select(Country.Region, Date, Daily_cases) %>%
mutate(Date = as.integer(Date)) %>%
nest(-Country.Region) %>%
mutate(
data = map(data, mutate_mapping, hcaes(x = Date, y = Daily_cases), drop = TRUE),
data = map(data, list_parse)
) %>%
rename(tt_nestdata = data)
tt_inner
tt_daily <- left_join(tt_base, tt_inner, by = "Country.Region")
tt_daily
tt_plt2 <- hchart(
tt_daily,
"point",
hcaes(x = Confirmed, y = Daily_cases, name = Country.Region,
size = Deaths, group = continent, name = Country.Region)
) %>%
# hc_yAxis(type = "logarithmic") %>%
hc_tooltip(
useHTML = TRUE,
headerFormat = "<b>{point.key}</b>",
pointFormatter = tooltip_chart(accesor = "tt_nestdata")
) %>%
hc_title(text = "Confirmed Vs Daily Cases as of latest Date (-US)") %>%
hc_subtitle(text = "Size of bubble based on Deaths <br> (hover over Bubble to view ttchart: Daily cases growth)") %>%
hc_caption(text = "created by ViSa!!")
tt_plt2
saveWidget(tt_plt2, "tt_world_cases-less_US.html")
saveWidget(tt_plt2, "tt_world_cases-less_US2.html", background = "#FFEADB")